package com.qf;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsBrand;
import com.qf.service.AdminService;
import com.qf.service.BrandService;
import com.qf.service.CategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sunyamei
 * @version V1.0
 * @Project javase2203
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/7/27 15:12
 *
 *
 *  http://localhost:8083/admin/brand/list?page=1&limit=20&sort=add_time&order=desc
 */
@RestController
@RequestMapping("/brand")
@CrossOrigin("*")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @Autowired
    private  AdminService adminService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param id
     * @param name
     * @return
     */
    @GetMapping("/list")
    public  Object brandPage(Integer page,Integer limit,String sort,String order,Integer id,String name){

        IPage<DtsBrand> brandPage = brandService.queryBrandPage(page, limit, sort, order, id, name);

        //查询总条数
        long total = brandPage.getTotal();
        //查询每页页数
        List<DtsBrand> list = brandPage.getRecords();

        Map<String, Object> date = new HashMap<>();
        date.put("total", total);
        date.put("items", list);
        return ResponseUtil.ok(date);

    }

    /**
     * 删除
     * @param brand
     * @return
     */
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsBrand brand){
        brandService.delete(brand);
        return ResponseUtil.ok();

    }

    /**
     * 添加
     * @param brand
     * @return
     */
    @PostMapping("/create")
    public Object add(@RequestBody DtsBrand brand){
        brandService.add(brand);
        return  ResponseUtil.ok(brand);
    }

    /**
     * 修改
     * @param brand
     * @return
     */
    @PostMapping("/update")
    public  Object update(@RequestBody DtsBrand brand){
        brandService.update(brand);
        return  ResponseUtil.ok();
    }


    /**
     * 品牌制造商联动
     * @return
     */
    @RequestMapping("/catAndAdmin")
    public  Object finaAdminList(){
        List<CatVo> adminList = adminService.findAdmin();
        List<CatVo> catList = categoryService.findCat();

        Map<String, Object> data = new HashMap<>();
        data.put("adminList", adminList);
        data.put("categoryList", catList);
        return  ResponseUtil.ok(data);
    }


}
